package com.ziyun.erp.web.api.quoteFactoryMaterial;

import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.quoteAttribute.entity.QuoteAttributeOptionEntity;
import com.ziyun.erp.modules.quoteAttribute.service.QuoteAttributeOptionService;
import com.ziyun.erp.modules.quoteFactoryMaterial.entity.ApiMaterialEntity;
import com.ziyun.erp.modules.quoteFactoryMaterial.entity.QuoteFactoryMaterialEntity;
import com.ziyun.erp.modules.quoteFactoryMaterial.entity.QuoteFactoryMaterialVO;
import com.ziyun.erp.modules.quoteFactoryMaterial.service.QuoteFactoryMaterialService;
import io.renren.common.annotation.SysLog;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.sys.entity.SysUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author jiangli
 * @date 2018-10-18 16:23
 */
@RestController
@RequestMapping("port/quoteFactoryMaterial/quotefactorymaterial")
public class QuoteFactoryMaterialControllerApi {

    @Autowired
    private QuoteFactoryMaterialService quoteFactoryMaterialService;
    @Autowired
    private QuoteAttributeOptionService quoteAttributeOptionService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestBody Map paramBodyMap) {
        PageUtils page = quoteFactoryMaterialService.queryPage(paramBodyMap);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info")
    public R info(@RequestBody Map paramBodyMap) {
        Object id = paramBodyMap.get("id");
        QuoteFactoryMaterialEntity quoteFactoryMaterial = quoteFactoryMaterialService.selectOneById((Integer) id);
        return R.ok().put("quoteFactoryMaterial", quoteFactoryMaterial);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ApiMaterialEntity apiMaterialEntity) {
        return quoteFactoryMaterialService.insertQuoteFactoryMaterial(apiMaterialEntity.getQuoteFactoryMaterialList(), Math.toIntExact(apiMaterialEntity.getFactoryId()),apiMaterialEntity.getUsername());
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ApiMaterialEntity apiMaterialEntity) {
        return quoteFactoryMaterialService.updateQuoteFactoryMaterialById(apiMaterialEntity.getQuoteFactoryMaterialList(),Math.toIntExact(apiMaterialEntity.getFactoryId()),apiMaterialEntity.getUsername());
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Map paramBodyMap) {
        Integer[] ids = (Integer[]) paramBodyMap.get("ids");
        quoteFactoryMaterialService.deleteByIds(ids);

        return R.ok();
    }

    /**
     * 增加克重弹窗 根据工厂id和选项id联合查询某工厂的材料克重
     */
    @RequestMapping("addWeight")
    public List<QuoteFactoryMaterialEntity> addWeight(@RequestBody Map paramBodyMap) {
        Integer factoryId = (Integer) paramBodyMap.get("factoryId");
        String optionKey = (String) paramBodyMap.get("optionKey");

        return quoteFactoryMaterialService.findWeightList(factoryId, optionKey, "shangwuyinshua");
    }

    /**
     * 启用/禁用
     */
    @RequestMapping("/setStart")
    public R setStart(@RequestBody Map paramBodyMap) {
        Integer id = (Integer) paramBodyMap.get("id");
        Boolean value = (Boolean) paramBodyMap.get("value");
        quoteFactoryMaterialService.setStart(id, value);
        return R.ok();
    }


    //页面leftTable数据
    @RequestMapping("/getLeftTableData")
    public  List<QuoteFactoryMaterialVO> getLeftTableData(@RequestBody Map paramBodyMap) {
        Integer factoryId = (Integer) paramBodyMap.get("factoryId");
        List<QuoteFactoryMaterialVO> leftTableData = quoteFactoryMaterialService.getLeftTableData(factoryId);
        return leftTableData;
    }

    //删除
    @RequestMapping("/delWeight")
    public R delWeight(@RequestBody Map paramBodyMap) {
        String optionName=(String) paramBodyMap.get("optionName");
        Integer factoryId = (Integer) paramBodyMap.get("factoryId");
        return quoteFactoryMaterialService.delWeight(optionName,factoryId);
    }

    /**
     * 得到材料选项集合
     */
    @RequestMapping("/getMaterialList")
    public List<QuoteAttributeOptionEntity> getMaterialList() {
        return quoteAttributeOptionService.getMaterialList();
    }

    //设为特种纸
    @RequestMapping("/specialPaper")
    public R specialPaper(@RequestBody Integer[] ids) {
        quoteFactoryMaterialService.specialPaper(ids);

        return R.ok();
    }

    //取消特种纸
    @RequestMapping("/unSpecialPaper")
    public R unSpecialPaper(@RequestBody Integer[] ids) {
        quoteFactoryMaterialService.unSpecialPaper(ids);

        return R.ok();
    }
}
